﻿package mortal.game.view.msgbroad
{
    import com.gengine.utils.pools.*;
    import com.greensock.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.component.gconst.*;
    import mortal.game.manager.msgTip.*;

    public class MouseTipItem extends Sprite
    {
        private var _iconBmp:Bitmap;
        private var _bitmap:Bitmap;
        private var _text:String;
        private var _textField:TextField;
        private var _matrix:Matrix;
        private var _targetIndex:int;
        private var _showTweenMax:TweenMax;
        private var _timeOut:int;
        private var _alphaTweenMax:TweenMax;
        private var _toStay:Boolean = false;
        private static var indexTip:Dictionary = new Dictionary();

        public function MouseTipItem()
        {
            this._textField = new TextField();
            this._textField.selectable = false;
            this._textField.mouseEnabled = false;
            this._textField.filters = [FilterConst.nameGlowFilter];
            var _loc_1:* = new GTextFormat(FontUtil.defaultName, 13, 16777215, false, null, null, null, null, TextFormatAlign.LEFT);
            _loc_1.letterSpacing = 2;
            this._textField.defaultTextFormat = _loc_1;
            this._textField.autoSize = TextFieldAutoSize.LEFT;
            this._matrix = new Matrix();
            this._matrix.tx = 45;
            this._matrix.ty = 7;
            this._iconBmp = new Bitmap();
            addChild(this._iconBmp);
            return;
        }// end function

        public function updateText(param1:String, param2:MsgRollTipsType) : void
        {
            this.disposeBitmap();
            this._iconBmp.bitmapData = GlobalClass.getBitmapData(param2.iconName);
            this._text = param1;
            this._textField.text = param1;
            var _loc_3:* = this._textField.textWidth + 80;
            var _loc_4:* = Math.max(33, this._textField.textHeight + 15);
            var _loc_5:* = new BitmapData(_loc_3, _loc_4, true, 16777215);
            var _loc_6:* = ResourceConst.getScaleBitmap(param2.bgName);
            _loc_6.width = _loc_3;
            _loc_6.height = _loc_4;
            _loc_5.draw(_loc_6);
            _loc_5.draw(this._textField, this._matrix);
            _loc_6.dispose();
            if (!this._bitmap)
            {
                this._bitmap = new Bitmap(_loc_5);
            }
            this._bitmap.bitmapData = _loc_5;
            this._bitmap.y = int((this._iconBmp.height - this._bitmap.height) / 2);
            this.addChild(this._bitmap);
            return;
        }// end function

        private function disposeBitmap() : void
        {
            if (this._bitmap)
            {
                this._bitmap.bitmapData.dispose();
            }
            return;
        }// end function

        public function dispose() : void
        {
            this._text = "";
            this.disposeBitmap();
            this.alpha = 1;
            this.x = 0;
            this.y = 0;
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            this._toStay = false;
            this.removeFromDic(this);
            if (this._showTweenMax)
            {
                this._showTweenMax.kill();
                this._showTweenMax = null;
            }
            if (this._alphaTweenMax)
            {
                this._alphaTweenMax.kill();
                this._alphaTweenMax = null;
            }
            clearTimeout(this._timeOut);
            ObjectPool.disposeObject(this);
            return;
        }// end function

        public function get text() : String
        {
            return this._text;
        }// end function

        public function get targetIndex() : int
        {
            return this._targetIndex;
        }// end function

        public function tweenShow() : void
        {
            var _loc_1:* = this.y - 100;
            this._targetIndex = int(_loc_1 / 36);
            var _loc_2:* = int((x + 50) / 100);
            this.x = _loc_2 * 100;
            var _loc_3:int = 0;
            var _loc_4:int = 3;
            var _loc_5:Array = [];
            _loc_3 = this._targetIndex + 2;
            while (_loc_3 >= this._targetIndex)
            {
                
                if (indexTip[_loc_3])
                {
                    _loc_5.push(indexTip[_loc_3]);
                    delete indexTip[_loc_3];
                }
                _loc_3 = _loc_3 - 1;
            }
            indexTip[this._targetIndex] = this;
            _loc_3 = this._targetIndex - 1;
            while (_loc_5.length)
            {
                
                if (indexTip[_loc_3])
                {
                    _loc_5.push(indexTip[_loc_3]);
                    delete indexTip[_loc_3];
                }
                (_loc_5.shift() as MouseTipItem).upTargetIndex(_loc_3);
                _loc_3 = _loc_3 - 1;
            }
            this.tweenShowStart();
            return;
        }// end function

        private function tweenShowStart() : void
        {
            var _loc_1:* = this._targetIndex * 36;
            this._showTweenMax = TweenMax.to(this, 0.5, {y:_loc_1, onComplete:this.stay});
            return;
        }// end function

        private function stay() : void
        {
            this._toStay = true;
            this._timeOut = setTimeout(this.tweenAplha, 600);
            return;
        }// end function

        private function tweenAplha() : void
        {
            this._alphaTweenMax = TweenMax.to(this, 0.5, {alpha:0, onComplete:this.dispose});
            return;
        }// end function

        public function upTargetIndex(param1:int) : void
        {
            this._targetIndex = param1;
            indexTip[this._targetIndex] = this;
            var _loc_2:* = this._targetIndex * 36;
            if (!this._toStay)
            {
                this._showTweenMax.kill();
                this._showTweenMax = TweenMax.to(this, 0.3, {y:_loc_2, onComplete:this.stay});
            }
            else
            {
                this._showTweenMax = TweenMax.to(this, 0.3, {y:_loc_2});
            }
            return;
        }// end function

        private function removeFromDic(param1:MouseTipItem) : void
        {
            var _loc_2:* = undefined;
            for (_loc_2 in indexTip)
            {
                
                if (indexTip[_loc_2] == this)
                {
                    delete indexTip[_loc_2];
                    return;
                }
            }
            return;
        }// end function

    }
}
